package com.jy.adscm.operation.pebase.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeDocument;
import com.jy.adscm.operation.pebase.bean.PeDocumentLabel;
import com.jy.adscm.operation.pebase.bean.PeLabel;
import com.jy.adscm.operation.pebase.bean.QueryPeDocmentLabel;
import com.jy.adscm.operation.pebase.mapper.mysql.PeDocumentLabelMapper;
import com.jy.adscm.operation.pebase.mapper.mysql.PeDocumentMapper;
import com.jy.adscm.operation.pebase.mapper.mysql.PeLabelMapper;
import com.jy.adscm.operation.pebase.service.PeDocumentLabelService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-04-18
 */
@Service
public class PeDocumentLabelServiceImpl extends ServiceImpl<PeDocumentLabelMapper, PeDocumentLabel> implements PeDocumentLabelService {
     @Autowired
     PeDocumentLabelMapper peDocumentLabelMapper;
     @Autowired
    PeDocumentMapper peDocumentMapper;
   @Autowired
    JyUserMapper jyUserMapper;
   @Autowired
    PeLabelMapper peLabelMapper;
    @Override
    public Page<PeDocument> selectDocumentByLabels(Page<PeDocument> page, QueryPeDocmentLabel queryPeDocmentLabel) throws AdscmSystemException {
        HashSet<Integer> dids=new HashSet<>();
        Wrapper<PeDocument> peDocumentWrapper=null;
        if (queryPeDocmentLabel.getLabels()!=null&&queryPeDocmentLabel.getLabels().length>0 ){
            peDocumentWrapper=  new EntityWrapper<>();
            String[] labels = queryPeDocmentLabel.getLabels();
            Wrapper<PeDocumentLabel> peDocumentLabelWrapper=new EntityWrapper<>();
            for (int i = 0; i < labels.length; i++) {
                if (i==0){
                    peDocumentLabelWrapper.eq("lid",labels[i]);
                    List<PeDocumentLabel> peDocumentLabels = peDocumentLabelMapper.selectList(peDocumentLabelWrapper);
                    if (peDocumentLabels!=null &&peDocumentLabels.size()>0){
                        for (PeDocumentLabel peDocumentLabel : peDocumentLabels) {
                            dids.add(peDocumentLabel.getDid());
                        }
                    }
                }else {
                    //第二次查询
                    HashSet<Integer> temp=new HashSet<>();
                    for (Integer did : temp) {
                        peDocumentLabelWrapper.eq("did",did);
                        peDocumentLabelWrapper.eq("lid",labels[i]);
                        List<PeDocumentLabel> peDocumentLabels = peDocumentLabelMapper.selectList(peDocumentLabelWrapper);
                        if (!(peDocumentLabels!=null&&peDocumentLabels.size()>0)){
                            dids.remove(did);
                        }
                    }
                }

                }
           if (dids.size()==0){
               return page;
           }else {
               peDocumentWrapper.in("id",dids);
           }

        }
        RowBounds rowBounds=new RowBounds((queryPeDocmentLabel.getCurrentPage()-1)*queryPeDocmentLabel.getPageSize(),queryPeDocmentLabel.getPageSize());
        List<PeDocument> peDocuments = peDocumentMapper.selectPage(rowBounds, peDocumentWrapper);
        for (PeDocument peDocument : peDocuments) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(peDocument.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(peDocument.getCreator());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    peDocument.setCreatorName(jyUser.getName());
                }else {
                    peDocument.setCreatorName(peDocument.getCreator());
                }
            }
        }
        page.setRecords(peDocuments);
        page.setTotal(peDocumentMapper.selectCount(peDocumentWrapper));
        return page;
    }

    @Override
    public List<PeLabel> selectLabelsByDid(String did) {

        List<PeLabel> peLabels =new ArrayList<>();
       Wrapper<PeDocumentLabel>   peDocumentLabelWrapper =new EntityWrapper<>();
        peDocumentLabelWrapper.eq("did",did);
        List<PeDocumentLabel> peDocumentLabels = peDocumentLabelMapper.selectList(peDocumentLabelWrapper);
        for (PeDocumentLabel peDocumentLabel : peDocumentLabels) {

            PeLabel peLabel = peLabelMapper.selectById(peDocumentLabel.getLid());
            if (AdscmBaseUtils.getInstance().isNotEmpty(peLabel,null)){
                peLabel.setRelationId(peDocumentLabel.getId());
                peLabels.add(peLabel);
            }

        }

        return peLabels;
    }
}
